<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Requests\GoodsRequest;
use App\Http\Controllers\Controller;
use App\Model\Goods;
use App\Model\Category;
use DB,Session,Input,Image,File;

class GoodsController extends Controller
{
    /**
     * Display a listing of the resource.
     * 商品列表
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $goods_name = Input::get('goods_name','');
        Input::flashOnly(['goods_name'=>$goods_name]);
        $goodslist = Goods::getGoodsList(1, $goods_name);
        return view("admin.goods.index", compact('goodslist'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $category = Category::getList();
        return view('admin.goods.add', compact('category'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(GoodsRequest $request)
    {
        $data = $request->except('_token');
        $goods = new Goods();
        $res = $goods->add($data);
        if(false !== $res){
            return redirect('/admin/goods');
        } else {
            return back()->with(['error'=>'商品添加失败']);
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $category = Category::getList();
        $goods = DB::table('goods')->where('id', $id)->first();
        $attrs = DB::table('goods_attr')->where('goods_id', $id)->get();
        return view('admin.goods.edit', compact('goods', 'category', 'attrs'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(GoodsRequest $request, $id)
    {
        $data = $request->except('_token','_method');
        $res = Goods::where('id', $id)->update($data);
        if(false !== $res){
            return back();
        } else {
            return back()->with(['errors'=>'修改失败']);
        }
    }

    /**
     * Remove the specified resource from storage.
     * 
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id=0)
    {
        $goods = new Goods();
        $data = ['goods_status'=>-1];
        if($id == 0){
            $id = Input::get('id');
        }
        if(is_array($id)){
            $action = '批量';
            $res = $goods->whereIn('id', $id)->update($data);
        } else {
            $action = '';
            $res = $goods->where('id', $id)->update($data);
        }
        
        if(false !== $res){
            return response()->json(['status'=>1, 'info'=>$action . '删除成功']);
        } else {
            return response()->json(['status'=>0, 'info'=>$action . '删除失败']);
        }
    }
    
    // 上传单张图片
    public function uploadone(Request $request){
        // 获取图片，并上传
        $file = $request->file('mallPic');
        $suffix = $file->getClientOriginalExtension();
        $savepath = './uploads/goodsimg/' . date('Ymd');
        if(!is_dir($savepath)){
            File::makeDirectory($savepath,  0777, true);
        }
        
        $rename = date('YmdHis') . mt_rand(1000,9999) . '.' . $suffix;
        $result = $file->move($savepath, $rename);
        
        if($result){
            $bigImg = $savepath . '/' . $rename;
            $thumbImg = $savepath . '/' . 'thumb_' . $rename;
            // 图像处理
            // 商品大图 800*800
            Image::make($bigImg)->resize(800, 800)->save($bigImg);
            Image::make($bigImg)->resize(180, 180)->save($thumbImg);
            // 返回
            return response()->json(['savepath'=>ltrim($bigImg,'.'), 'thumbsavepath'=>ltrim($thumbImg,'.')]);
        } else {
            return response()->json(['savepath'=>'','thumbsavepath'=>'']);
        }
    }
    
    /**
     * 上架下架
     * @return type
     */
    public function issale()
    {
        $id = Input::get('id');
        $status = Input::get('status');
        
        $goods = new Goods();
        $goods_saletime = date('Y-m-d H:i:s');
        $data = ['is_sale'=>$status];
        
        if($status == 1){
            $action = '上架';
            $data['goods_saletime'] = date('Y-m-d H:i:s');
        } else {
            $action = '下架';
        }
        
        if(is_array($id)){
            $action = '批量' . $action;
            $res = $goods->whereIn('id', $id)->update($data);
        } else {
            $res = $goods->where('id', $id)->update($data);
        }
        
        if(false !== $res){
            return response()->json(['status'=>1, 'info'=>$action . '修改成功']);
        } else {
            return response()->json(['status'=>0, 'info'=>$action . '修改失败']);
        }
    }
}
